﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Microsoft.SharePoint.Client;

namespace SPColumnManager
{
    public partial class MainForm : System.Windows.Forms.Form
    {
        ColumnPropertyEditor cpe;
        
        public MainForm()
        {
            InitializeComponent();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {

            cpe = new ColumnPropertyEditor(txtSiteUrl.Text);
            //Will need to add the logic to change user credentials here
            cbLists.Items.Clear();
            ListCollection lists = cpe.getListCollection();
            foreach (List list in lists)
            {
                if (!list.Hidden)
                {
                    cbLists.Items.Add(list.Title);
                }
            }
            cbLists.SelectedIndex = 0;

        }

        private void btnPopList_Click(object sender, EventArgs e)
        {

            lblExecuting.Visible = true;


            string listName = cbLists.SelectedItem.ToString();
            gridColumns.Rows.Clear();

            FieldCollection fields = cpe.getListFieldCollection(listName);

            foreach (Field field in fields)
            {
                if (!field.Hidden && !field.Sealed && !field.ReadOnlyField)
                {   
                    bool showInNewForm = true;
                    bool showInEditForm = true;
                    bool showInViewForm = true;

                    if (field.SchemaXml.Contains("ShowInNewForm=\"FALSE\""))
                    {
                        showInNewForm = false;
                    }
                    if (field.SchemaXml.Contains("ShowInEditForm=\"FALSE\""))
                    {
                        showInEditForm = false;
                    }
                    if (field.SchemaXml.Contains("ShowInViewForm=\"FALSE\""))
                    {
                        showInViewForm = false;
                    }
                    gridColumns.Rows.Add(field.Title, showInNewForm, showInEditForm, showInViewForm);
                    
                }
            }


            lblExecuting.Visible = false;

        }

        private void gridColumns_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            lblExecuting.Visible = true;
            bool cellValue = (bool)gridColumns[e.ColumnIndex, e.RowIndex].Value;

            string columnName = gridColumns.Columns[e.ColumnIndex].Name;
            string fieldName = (string)gridColumns[0, e.RowIndex].Value;

            cpe.setPropertyValue(fieldName, columnName, cellValue);

            lblExecuting.Visible = false;
        }

        private void gridColumns_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox1 ab1 = new AboutBox1();
            ab1.Show();
        }


    }
}
